﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="../../js/header.js"></script>
    <link href="/lib/ztree/ztreestyle.css" rel="stylesheet" />
    <link href="/lib/icheck/skins/all.css" rel="stylesheet" />
    <style>
        .usermenu {
            background-color: #f5f5f5;
            padding: 5px 20px;
            overflow-x: hidden;
            overflow-y: auto;
            height: 215px;
        }
    </style>
    <script type="text/javascript" src="/lib/ztree/jquery.ztree.all.js"></script>
</head>
<body style="background-color: white;">
    <section class="container-fluid" style="background-color: white;">

        <ul class="nav nav-tabs m15_t" id="myTab">
            <li class="active"><a href="#home" data-toggle="tab">基本信息</a></li>
            <li><a href="#permissions" data-toggle="tab">角色权限</a></li>
        </ul>
        <form class="form-horizontal m15_t" id="form">
            <div class="tab-content">
                <input type="hidden" id="rId" name="rId" value="0">
                <div class="tab-pane fade in active" id="home">
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>角色名称：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="rName" maxlength="40" autocomplete="off" placeholder="长度必须介于 3 和 40 之间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>所属公司：</label>
                        <div class="wd250 m15_l">
                            <select class="form-control input-sm wd240" name="rCompanyId" style="display: inline-block; width: auto;">
                                <option value="0">根目录</option>
                            </select>
                        </div>
                    </div>
                </div>
                <!--菜单-->
                <div class="tab-pane fade" id="permissions">
                    <div class="form-group">
                        <div class="usermenu">
                            <div class="zTreeDemoBackground left">
                                <ul id="ztree" class="ztree"></ul>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </form>

    </section>

    <script type="text/javascript">
        //角色状态
        var roleStatus =
            {
                show: 1,
                hide: 2
            };

        var validator = null;

        var rCompanyId = $("select[name='rCompanyId']");

        var setting = {
            check: {
                enable: true,
                chkboxType: { "Y": "p", "N": "ps" }
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            }
        };

        $(function () {
            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Role/GetAddOrEdit/",
                data: null,
                success: function (response) {
                },
                error: function (e) {
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试");
                }
            });

            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Role/GetCompany/",
                data: null,
                success: function (response) {
                    if (response.success) {
                        FlashPay.Util.SetInputVal("rId", 0);
                        FlashPay.Util.SetInputVal("rName", "")

                        rCompanyId.empty();
                        if (response.data != null) {
                            $.each(response.data, function (i, item) {
                                rCompanyId.append("<option value='" + item.companyID + "'>" + item.companyName + "</option >");
                            });
                        }
                        else {
                            //rCompanyId.append("<option value='0'></option>");
                        }
                    } else {

                    }
                },
                error: function (e) {
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试");
                }
            });

            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    rName: {
                        required: true,
                        rangelength: [3, 40]
                    }
                },
                messages: {
                    rName: {
                        required: "长度必须介于 3 和 40 之间",
                        rangelength: "长度必须介于 3 和 40 之间",
                    }
                },
            });

            InitZTree();
        })
        var ztree;
        //初始化ZTree
        function InitZTree() {

            var roleSysNo = $("input[name='rId']").val();


            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Role/GetMenuPermissionTree",
                data: { mType: FlashPay.Util.Platform, roleSysNo: FlashPay.Util.GetQueryString("id") },
                success: function (response) {
                    if (response != null) {
                        //更具返回结果初始化zTree
                        ztree = $.fn.zTree.init($("#ztree"), setting, response);
                        //默认为收起所以节点
                        ztree.ExpandAllStatus = false;
                    }
                },
                error: function (e) {
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试");
                }
            });
        }

        function save() {

            if (!validator.form()) {
                return false;
            }

            var role = {};
            role.rId = $("input[name='rId']").val();
            role.rName = $("input[name='rName']").val();
            role.rCompanyId = $("select[name='rCompanyId']").find("option:selected").val();
            role.rStatus = roleStatus.show;

            var selectedMenuNodes = ztree.getCheckedNodes(true);
            var authorizeArray = new Array();
            if (selectedMenuNodes.length > 0) {
                for (var i = 0; i < selectedMenuNodes.length; i++) {
                    var authorizeId = selectedMenuNodes[i].id.replace("m_", "").replace("p_", "");
                    var authorizeType = selectedMenuNodes[i].nodetype;

                    var authorize = {
                        authPid: authorizeId,
                        authRid: role.rId,
                        authType: authorizeType,
                    };
                    authorizeArray.push(authorize);
                }
            }

            FlashPay.Util.Ajax({
                url: '/Role/SaveOrEdit',
                type: 'POST',
                data: { role: role, authorize: authorizeArray },
                success: function (response) {
                    if (response.success) {
                        FlashPay.UI.CloseDialog();
                        FlashPay.UI.DialogOpener().doSearch();
                        FlashPay.UI.DialogOpener().Tip_success("操作成功！");
                    } else {
                        FlashPay.UI.Tip_short_warning("操作失败！" + response.errorMessage);
                    }
                },
                error: function (json) {
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试。");
                }
            });
        }
    </script>
</body>
</html>